package cn.stylefeng.guns.modular.hx.storage.service.impl;

import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import cn.stylefeng.guns.modular.bi.entity.vo.DpkcQueryIby;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPutgoodsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPutgoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicPutgoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicTrayService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicGoods;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicPositionGoods;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicGoodsParam;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicPositionGoodsParam;
import cn.stylefeng.guns.modular.hx.basic.model.result.HxBasicGoodsResult;
import cn.stylefeng.guns.modular.hx.basic.model.result.HxBasicPositionGoodsResult;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicGoodsService;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicPositionGoodsService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageStock;
import cn.stylefeng.guns.modular.hx.storage.mapper.HxStorageStockMapper;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageStockResult;
import  cn.stylefeng.guns.modular.hx.storage.service.HxStorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2023-08-15
 */
@Service
@Slf4j
public class HxStorageStockServiceImpl extends ServiceImpl<HxStorageStockMapper, HxStorageStock> implements HxStorageStockService {
	
    @Resource
    private BasicWarehouseService warehouseService;
    
    @Resource
    private BasicPositionService positionService;
    
    @Resource
    private BasicPutgoodsService basicPutgoodsService;
    
    @Resource
    private BasicCustomerService customerService;
    
    @Resource
    private HxBasicGoodsService hxBasicGoodsService;
    
    @Resource
    private BasicPutgoodsService putgoodsService;
    
    @Resource
    private HxBasicPositionGoodsService hxPositionGoodsService;
    
    @Override
    public void add(HxStorageStockParam param){
        HxStorageStock entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional
    public void addItem(HxStorageStockParam param,User user){
        HxStorageStock storageStock = new HxStorageStock();
        storageStock.setStockState("正常");//状态
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        Long operateId = param.getOperateId();
        String trayCode = getOperateIdOrderPrefix(user)+"-";
        storageStock.setTransferTime(new Date());
        storageStock.setPutDate(DateUtil.getDate());
        storageStock.setDeptId(deptId);//部门id
        storageStock.setDeliverId(deliverId);//交付id
        storageStock.setOperateId(operateId);

        Long customerId = param.getCustomerId();//客户id
        if (ToolUtil.isEmpty(customerId))
            throw new EmptyStackException();
        storageStock.setCustomerId(customerId);
        BasicCustomer basicCustomer = customerService.getById(customerId);
        storageStock.setCustomerName(basicCustomer.getCustomerName());//客户名称
        storageStock.setOperateName(basicCustomer.getCustomerOperateName());

        Long warehouseId = param.getWarehouseId();
        if (ToolUtil.isEmpty(warehouseId))
            throw new EmptyStackException();
        BasicWarehouse warehouse = warehouseService.getById(warehouseId);
        storageStock.setWarehouseName(warehouse.getWarehouseName());//仓库名称
        storageStock.setWarehouseId(warehouseId);//仓库id

        Long positionId = param.getPositionId();
        if (ToolUtil.isEmpty(positionId))
            throw new EmptyStackException();
        BasicPosition position = positionService.getById(positionId);
        storageStock.setPositionNumber(position.getPositionNumber());//库位编号
        storageStock.setPositionId(positionId);
        storageStock.setPositionType(position.getPositionType());
        storageStock.setAreaId(Long.valueOf(position.getPositionAreaId()));
        storageStock.setAreaNumber(position.getAreaNumber());
        storageStock.setAreaName(position.getPositionAreaName());
        storageStock.setTrayNumber(trayCode+DateUtil.getRandomNumber());//项目代码+五位随机数

        //查询商品
        Long goodsId = param.getGoodsId();
        if (ToolUtil.isEmpty(goodsId))
            throw new EmptyStackException();
        HxBasicGoods hxBasicGoods = hxBasicGoodsService.getById(goodsId);
        storageStock.setGoodsId(goodsId);
        storageStock.setGoodsName(hxBasicGoods.getGoodsName());
        storageStock.setGoodsNumber(hxBasicGoods.getGoodsNumber());
        storageStock.setGoodsPcode69(hxBasicGoods.getGoodsPcode69());
        storageStock.setGoodsBigtypeId(hxBasicGoods.getGoodsBigtypedictId());
        storageStock.setGoodsSpecs(hxBasicGoods.getGoodsSpecs());
        storageStock.setGoodsBigtype(hxBasicGoods.getGoodsBigtype());

        storageStock.setJibenUnit(hxBasicGoods.getJibenUnit());
        storageStock.setKcUnit(hxBasicGoods.getKcUnit());
        storageStock.setKcRate(hxBasicGoods.getKcRate());
        storageStock.setCgUnit(hxBasicGoods.getCgUnit());
        storageStock.setCgRate(hxBasicGoods.getCgRate());
        storageStock.setPsUnit(hxBasicGoods.getPsUnit());
        storageStock.setPsRate(hxBasicGoods.getPsRate());
        storageStock.setPfUnit(hxBasicGoods.getPfUnit());
        storageStock.setPfRate(hxBasicGoods.getPfRate());
        storageStock.setGoodsWarranty(hxBasicGoods.getGoodsWarranty());

        if (ToolUtil.isEmpty(param.getDetailedDate()))
            throw new EmptyStackException();
        storageStock.setDetailedDate(param.getDetailedDate());

        String kcNum = param.getKcNum();
		BigDecimal goodsVolume = new BigDecimal(hxBasicGoods.getGoodsVolume());
		BigDecimal goodsWeight = new BigDecimal(hxBasicGoods.getGoodsWeight());
		BigDecimal goodsChargingCg = new BigDecimal(hxBasicGoods.getGoodsChargingCg());
		BigDecimal ratiobig = new BigDecimal(hxBasicGoods.getKcRate());
		BigDecimal cgRate = new BigDecimal(hxBasicGoods.getCgRate());
		BigDecimal goodsPrice = ratiobig.multiply(goodsChargingCg).setScale(3, BigDecimal.ROUND_HALF_DOWN);
		BigDecimal quantitybig = new BigDecimal(kcNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
		if(ratiobig.compareTo(cgRate) == -1) {
			//如果配送换算率 小于 采购换算率：  配送体积  =  采购体积/（采购换算率/配送换算率）
			BigDecimal div = cgRate.divide(ratiobig,3, BigDecimal.ROUND_HALF_DOWN);
			BigDecimal divv_v = goodsVolume.divide(div,4, BigDecimal.ROUND_HALF_DOWN);
			goodsVolume = divv_v;
			BigDecimal divv_w = goodsWeight.divide(div,3, BigDecimal.ROUND_HALF_DOWN);
			goodsWeight = divv_w;
		}
		if(ratiobig.compareTo(cgRate) == 1) {
			//如果配送换算率 大于 采购换算率：  配送体积  =  采购体积*（配送换算率/采购换算率）
			BigDecimal div = ratiobig.divide(cgRate,3, BigDecimal.ROUND_HALF_DOWN);
			BigDecimal divv_v = goodsVolume.multiply(div).setScale(4, BigDecimal.ROUND_HALF_DOWN);
			goodsVolume = divv_v;
			BigDecimal divv_w = goodsWeight.multiply(div).setScale(3, BigDecimal.ROUND_HALF_DOWN);
			goodsWeight = divv_w;
		}
        storageStock.setKcNum(quantitybig+"");
        storageStock.setJibenNum(new BigDecimal(storageStock.getKcNum()).multiply(new BigDecimal(storageStock.getKcRate())).setScale(3, BigDecimal.ROUND_HALF_DOWN));
        storageStock.setGoodsWeight(goodsWeight.toString());
        storageStock.setStockWeight(quantitybig.multiply(goodsWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
        storageStock.setGoodsVolume(goodsVolume.toString());
        storageStock.setStockVolume(quantitybig.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
        storageStock.setGoodsPrice(goodsPrice.toString());
        storageStock.setStockMoney(quantitybig.multiply(goodsPrice).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());

        //完成库位表商品绑定
        if(ToolUtil.isNotEmpty(position.getPositionType())&&position.getPositionType().equals("拣货位")) {
            HxBasicPositionGoodsParam positionGoodsParam = new HxBasicPositionGoodsParam();
            positionGoodsParam.setWarehouseId(warehouseId);
            positionGoodsParam.setPositionId(positionId);
            positionGoodsParam.setPositionType("拣货位");
            positionGoodsParam.setGoodsId(goodsId);
            HxBasicPositionGoodsResult positionGoodsResult = hxPositionGoodsService.findBySpec(positionGoodsParam);
            if (ToolUtil.isEmpty(positionGoodsResult)){
                HxBasicPositionGoods hxBasicPositionGoods = new HxBasicPositionGoods();
                BeanUtil.copyProperties(hxBasicGoods,hxBasicPositionGoods);
                hxBasicPositionGoods.setGoodsId(goodsId);
                hxBasicPositionGoods.setPositionId(positionId);
                hxBasicPositionGoods.setWarehouseId(warehouseId);
                hxBasicPositionGoods.setAreaId(storageStock.getAreaId());
                hxBasicPositionGoods.setAreaName(storageStock.getAreaName());
                hxBasicPositionGoods.setPositionNumber(position.getPositionNumber());
                hxBasicPositionGoods.setPositionPickingorder(position.getPositionPickingorder());
                hxPositionGoodsService.save(hxBasicPositionGoods);
            }
        }
        this.save(storageStock);
    }

    @Override
    public void delete(HxStorageStockParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(HxStorageStockParam param){
        HxStorageStock oldEntity = getOldEntity(param);
        HxStorageStock newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HxStorageStockResult findBySpec(HxStorageStockParam param, Long operateId){
        return this.baseMapper.findBySpec(param,operateId);
    }

    @Override
    public HxStorageStockResult findBySpec1(HxStorageStockParam param) {
        return null;
    }

    @Override
    public List<HxStorageStockResult> findListBySpec(HxStorageStockParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public List<HxStorageStockResult> findListByStorageNumber(HxStorageStockParam param){
        return this.baseMapper.findListByStorageNumber(param);
    }

    @Override
    public List<HxStorageStockResult> findListByStorageNumber1(HxStorageStockParam param){
        return this.baseMapper.findListByStorageNumber1(param);
    }


    @Autowired
    public HxStorageStockMapper hxStorageStockMapper;
    @Override
    public LayuiPageInfo findPageBySpec(HxStorageStockParam hxStorageStockParam, String condition){
        //处理生产日期的起始和终止时间
        /*if (ToolUtil.isEmpty(hxStorageStockParam.getStartTime())&&
                ToolUtil.isNotEmpty(hxStorageStockParam.getEndTime())){
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime now = LocalDateTime.now().minusYears(10);
            hxStorageStockParam.setStartTime(now.format(dtf));
        }
        if (ToolUtil.isEmpty(hxStorageStockParam.getEndTime())&&
                ToolUtil.isNotEmpty(hxStorageStockParam.getStartTime())){
            hxStorageStockParam.setEndTime(DateUtil.getDate());
        }*/
        //构建分页条件，进行分页查询，获取数据
        Page<HxStorageStockParam> pageContext = getPageContext();
        IPage<HxStorageStockParam> page=null;
        //按模式和单位查询
        String queryMode = hxStorageStockParam.getQueryMode();
        String stockUnit = hxStorageStockParam.getStockUnit();
        if ("cg".equals(stockUnit) && "mx".equals(queryMode)){//1
            page = this.baseMapper.customPageList(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);
            });
            page.setRecords(list);
        }else if ("pf".equals(stockUnit) && "mx".equals(queryMode)){//2
            page = this.baseMapper.customPageList(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
            });
            page.setRecords(list);
        }else if ("ps".equals(stockUnit) && "mx".equals(queryMode)){//3
            page = this.baseMapper.customPageList(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
            });
            page.setRecords(list);
        }else if ("kc".equals(stockUnit) && "hz".equals(queryMode)){//4
            page = this.baseMapper.customPageList1(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
            });
            page.setRecords(list);
        }else if ("cg".equals(stockUnit) && "hz".equals(queryMode)){//5
            page = this.baseMapper.customPageList1(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);
            });
            page.setRecords(list);
        }else if ("pf".equals(stockUnit) && "hz".equals(queryMode)){//6
            page = this.baseMapper.customPageList1(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
            });
            page.setRecords(list);

        }else if ("ps".equals(stockUnit) && "hz".equals(queryMode)){//7
            page = this.baseMapper.customPageList1(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
            });
            page.setRecords(list);
        }else if ("kc".equals(stockUnit) && "kw".equals(queryMode)){//8
            page = this.baseMapper.customPageList2(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
            });
            page.setRecords(list);
        }else if ("cg".equals(stockUnit) && "kw".equals(queryMode)){//9
            page = this.baseMapper.customPageList2(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);
            });
            page.setRecords(list);
        }else if ("pf".equals(stockUnit) && "kw".equals(queryMode)){//10
            page = this.baseMapper.customPageList2(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
            });
            page.setRecords(list);

        }else if ("ps".equals(stockUnit) && "kw".equals(queryMode)){//11
            page = this.baseMapper.customPageList2(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
            });
            page.setRecords(list);
        }else {//0
            page = this.baseMapper.customPageList(pageContext, hxStorageStockParam,condition);
            List<HxStorageStockParam> list=page.getRecords();
            list.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
            });
            page.setRecords(list);
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout=400)
    public ResponseData getExceldata(MultipartFile file, HxStorageStockParam hxStorageStockParam, User user, ResponseData data) throws Exception {
        if (ToolUtil.isEmpty(hxStorageStockParam.getCustomerId()) && ToolUtil.isEmpty(hxStorageStockParam.getCustomerName())) {
            data.setSuccess(false);
            data.setMessage("Please Select Customer");
            return data;
        }
        Long customerId = hxStorageStockParam.getCustomerId();//客户id
        BasicCustomer basicCustomer = customerService.getById(customerId);
        if (ToolUtil.isEmpty(hxStorageStockParam.getWarehouseId()) && ToolUtil.isEmpty(hxStorageStockParam.getWarehouseId())) {
            data.setSuccess(false);
            data.setMessage("请选择仓库");
            return data;
        }
        Long warehouseId = hxStorageStockParam.getWarehouseId();
        int i = warehouseId.intValue();
        BasicWarehouse warehouseResult = warehouseService.getById(warehouseId);
        if (ToolUtil.isEmpty(hxStorageStockParam.getOperateId()) && ToolUtil.isEmpty(hxStorageStockParam.getOperateId())) {
            data.setSuccess(false);
            data.setMessage("请选择项目");
            return data;
        }

        String fileName = file.getOriginalFilename();
        if (fileName == null){
            data.setSuccess(false);
            data.setMessage("文件名不能为空");
            return data;
        }
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            data.setMessage("上传文件格式不正确");
            data.setSuccess(false);
        }

        boolean isExcel2003 = !fileName.matches("^.+\\.(?i)(xlsx)$");
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }

        Sheet sheet = wb.getSheetAt(0);
        List<HxStorageStock> list = new ArrayList<HxStorageStock>();
        List<HxBasicPositionGoodsParam> listpg = new ArrayList<HxBasicPositionGoodsParam>();
        Long deptId = user.getDeptId();
        Long operateId = user.getOperateId();
        Long deliverId = user.getDeliverId();
        String trayCode = getOperateIdOrderPrefix(user)+"-";
        String msg = "";
        Map<String, BasicPositionResult> positionMap = new HashMap<>();
        BasicPositionParam plistparam = new BasicPositionParam();
        List<BasicPositionResult> plist = positionService.findListBySpec(plistparam, null, null, hxStorageStockParam.getWarehouseId(), null);
        for (BasicPositionResult pr : plist) {
        	positionMap.put(pr.getPositionNumber(), pr);
		}
		Map<String, HxBasicGoodsResult> goodsMap = new HashMap<>();
		HxBasicGoodsParam hxgoodsp = new HxBasicGoodsParam();
		hxgoodsp.setGoodsCustomerId(basicCustomer.getId());
		List<HxBasicGoodsResult> hxg = hxBasicGoodsService.findListBySpec(hxgoodsp);
		for (HxBasicGoodsResult gr : hxg) {
			goodsMap.put(gr.getGoodsNumber(), gr);
		}
		Map<String, BasicPutgoodsResult> putgoodsMap = new HashMap<>();
		BasicPutgoodsParam hxputgoodsp = new BasicPutgoodsParam();
		hxputgoodsp.setPutgoodsCustomerId(basicCustomer.getId());
		hxputgoodsp.setUnitType("供应商");
		List<BasicPutgoodsResult> hxpg = basicPutgoodsService.findListBySpec(hxputgoodsp,null);
		for (BasicPutgoodsResult gr : hxpg) {
			putgoodsMap.put(gr.getPutgoodsName(), gr);
		}
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            HxStorageStock storageStock = new HxStorageStock();
            storageStock.setStockState("正常");//状态
            storageStock.setDeptId(deptId);//部门id
            storageStock.setDeliverId(deliverId);//交付id
            storageStock.setOperateId(operateId);
            storageStock.setPutDate(DateUtil.getDate());
            storageStock.setTransferTime(new Date());
            storageStock.setCustomerId(customerId);
            storageStock.setCustomerName(basicCustomer.getCustomerName());//客户名称
            storageStock.setOperateName(basicCustomer.getCustomerOperateName());
            storageStock.setWarehouseName(warehouseResult.getWarehouseName());//仓库名称
            storageStock.setWarehouseId(warehouseId);//仓库id

            String  positionNumber = getCellValue(row.getCell(0));
            String positionPickingorder = "";
            if (ToolUtil.isNotEmpty(positionNumber)) {
                storageStock.setPositionNumber(positionNumber);//库位编号
                BasicPositionResult result = positionMap.get(positionNumber);
                if (ToolUtil.isEmpty(result)){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
                	return ResponseData.error(positionNumber+"库位不存在！");
                }
                positionPickingorder = result.getPositionPickingorder();
                storageStock.setPositionId(Long.valueOf(result.getId()));
                storageStock.setPositionType(result.getPositionType());
                storageStock.setAreaId(Long.valueOf(result.getPositionAreaId()));
                storageStock.setAreaNumber(result.getAreaNumber());
                storageStock.setAreaName(result.getPositionAreaName());
                storageStock.setTrayNumber(trayCode+DateUtil.getRandomNumber());//项目代码+5位随机数
            } else {
                continue;
            }

            String  goodsNumber = getCellValue(row.getCell(1));
            HxBasicGoodsResult goodsResult =null;
            Long goodsId=null;
            if (ToolUtil.isNotEmpty(goodsNumber)) {
                //查询商品
                goodsResult = goodsMap.get(goodsNumber);
                if (ToolUtil.isEmpty(goodsResult)){
                	msg+=goodsNumber+"，";
                    continue;
                }
                goodsId = Long.valueOf(goodsResult.getId());
                storageStock.setGoodsId(goodsId);
                storageStock.setGoodsName(goodsResult.getGoodsName());
                storageStock.setGoodsNumber(goodsNumber);
                storageStock.setGoodsPcode69(goodsResult.getGoodsPcode69());
                storageStock.setGoodsSpecs(goodsResult.getGoodsSpecs());
                storageStock.setGoodsBigtypeId(goodsResult.getGoodsBigtypedictId());
                storageStock.setGoodsBigtype(goodsResult.getGoodsBigtype());

                storageStock.setJibenUnit(goodsResult.getJibenUnit());
                storageStock.setKcUnit(goodsResult.getKcUnit());
                storageStock.setKcRate(goodsResult.getKcRate());
                storageStock.setCgUnit(goodsResult.getCgUnit());
                storageStock.setCgRate(goodsResult.getCgRate());
                storageStock.setPsUnit(goodsResult.getPsUnit());
                storageStock.setPsRate(goodsResult.getPsRate());
                storageStock.setPfUnit(goodsResult.getPfUnit());
                storageStock.setPfRate(goodsResult.getPfRate());
                storageStock.setGoodsWarranty(goodsResult.getGoodsWarranty());
            } else {
                continue;
            }

            String  detailedDate = getCellValue(row.getCell(2));
            if (ToolUtil.isNotEmpty(detailedDate)){
                storageStock.setDetailedDate(detailedDate);
            } else {
            	storageStock.setDetailedDate(DateUtil.getDate());
            }
            String  kcNum = getCellValue(row.getCell(3));
            if (ToolUtil.isNotEmpty(kcNum)){
        		BigDecimal goodsVolume = new BigDecimal(goodsResult.getGoodsVolume());
        		BigDecimal goodsWeight = new BigDecimal(goodsResult.getGoodsWeight());
    			BigDecimal goodsChargingCg = new BigDecimal(goodsResult.getGoodsChargingCg());
    			BigDecimal ratiobig = new BigDecimal(goodsResult.getKcRate());
    			BigDecimal cgRate = new BigDecimal(goodsResult.getCgRate());
    			BigDecimal goodsPrice = ratiobig.multiply(goodsChargingCg).setScale(3, BigDecimal.ROUND_HALF_DOWN);
    			BigDecimal quantitybig = new BigDecimal(kcNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
    			if(ratiobig.compareTo(cgRate) == -1) {
    				//如果配送换算率 小于 采购换算率：  配送体积  =  采购体积/（采购换算率/配送换算率）
    				BigDecimal div = cgRate.divide(ratiobig,3, BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal divv_v = goodsVolume.divide(div,4, BigDecimal.ROUND_HALF_DOWN);
    				goodsVolume = divv_v;
    				BigDecimal divv_w = goodsWeight.divide(div,3, BigDecimal.ROUND_HALF_DOWN);
    				goodsWeight = divv_w;
    			}
    			if(ratiobig.compareTo(cgRate) == 1) {
    				//如果配送换算率 大于 采购换算率：  配送体积  =  采购体积*（配送换算率/采购换算率）
    				BigDecimal div = ratiobig.divide(cgRate,3, BigDecimal.ROUND_HALF_DOWN);
    				BigDecimal divv_v = goodsVolume.multiply(div).setScale(4, BigDecimal.ROUND_HALF_DOWN);
    				goodsVolume = divv_v;
    				BigDecimal divv_w = goodsWeight.multiply(div).setScale(3, BigDecimal.ROUND_HALF_DOWN);
    				goodsWeight = divv_w;
    			}
                storageStock.setKcNum(quantitybig+"");
                storageStock.setJibenNum(new BigDecimal(storageStock.getKcNum()).multiply(new BigDecimal(storageStock.getKcRate())).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                storageStock.setGoodsWeight(goodsWeight.toString());
                storageStock.setStockWeight(quantitybig.multiply(goodsWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                storageStock.setGoodsVolume(goodsVolume.toString());
                storageStock.setStockVolume(quantitybig.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                storageStock.setGoodsPrice(goodsPrice.toString());
                storageStock.setStockMoney(quantitybig.multiply(goodsPrice).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
            }else {
                continue;
            }
            String  setUnit = getCellValue(row.getCell(4));
            if (ToolUtil.isNotEmpty(setUnit)){
                BasicPutgoodsResult putgoodsResult = putgoodsMap.get(setUnit);
                if(putgoodsResult != null) {
                	storageStock.setSetUnit(hxStorageStockParam.getSetUnit());
                	storageStock.setSetUnitId(Long.valueOf(putgoodsResult.getId()));
                }
            }
            list.add(storageStock);
            if(list.size() == 500) {
                //保存到库存表
                this.saveBatch(list);
                list = new ArrayList<HxStorageStock>();
            }
            //完成库位表商品绑定
            if(storageStock.getPositionType().equals("拣货位")) {
            	HxBasicPositionGoodsParam positionGoodsParam = new HxBasicPositionGoodsParam();
            	positionGoodsParam.setWarehouseId(warehouseId);
            	Long positionId = storageStock.getPositionId();
            	positionGoodsParam.setPositionId(positionId);
            	positionGoodsParam.setPositionType("拣货位");
            	positionGoodsParam.setGoodsId(goodsId);
            	HxBasicPositionGoodsResult positionGoodsResult = hxPositionGoodsService.findBySpec(positionGoodsParam);
            	if (ToolUtil.isEmpty(positionGoodsResult)){
            		boolean bcylm = false;
            		for (HxBasicPositionGoodsParam bdym : listpg) {
						if(bdym.getPositionId().equals(positionId)&&bdym.getGoodsId().equals(goodsId)) {
							bcylm = true;
							break;
						}
					}
            		if(!bcylm) {
            			HxBasicPositionGoodsParam hxBasicPositionGoods = new HxBasicPositionGoodsParam();
            			BeanUtil.copyProperties(goodsResult,hxBasicPositionGoods);
            			hxBasicPositionGoods.setId(null);
            			hxBasicPositionGoods.setPositionId(positionId);
            			hxBasicPositionGoods.setWarehouseId(warehouseId);
            			hxBasicPositionGoods.setAreaId(storageStock.getAreaId());
            			hxBasicPositionGoods.setAreaName(storageStock.getAreaName());
            			hxBasicPositionGoods.setPositionNumber(positionNumber);
            			hxBasicPositionGoods.setPositionPickingorder(positionPickingorder);
            			hxBasicPositionGoods.setGoodsId(goodsId);
            			listpg.add(hxBasicPositionGoods);
            		}
            	}
            }
        }
        if(ToolUtil.isNotEmpty(msg)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
        	return ResponseData.error(msg+"商品不存在！");
        }
        if (list.size() > 0) {
            //保存到库存表
            this.saveBatch(list);
        }
        if (listpg.size() > 0) {
            //保存到库位商品关系
        	hxPositionGoodsService.saveBatchList(listpg);
        }
        data.setSuccess(true);
        return data;
    }

    @Override
    public HxStorageStockParam details(HxStorageStockParam hxStorageStockParam) {
        if ("ps".equals(hxStorageStockParam.getStockUnit()) && "mx".equals(hxStorageStockParam.getQueryMode())){
            HxStorageStock detail = this.baseMapper.selectById(hxStorageStockParam.getId());
            BigDecimal value = BigDecimal.valueOf(Double.parseDouble(detail.getKcNum()));
            BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(detail.getKcRate()));
            BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(detail.getPsRate()));
            BigDecimal multiply = value.multiply(value1);
            DecimalFormat df = new DecimalFormat("0.000");
            df.setRoundingMode(RoundingMode.HALF_UP);
            BigDecimal divide = multiply.divide(value2);
            String format = df.format(divide);

            HxStorageStockParam stockParam = new HxStorageStockParam();
            BeanUtil.copyProperties(detail,stockParam);
            stockParam.setPsNum(format);
            return stockParam;
        }else {
            return null;
        }
    }

    /**
     * 输入 星期三 五月 01 22:25:49 CST 2023
     * 输出 2023-05-01 (输出方法的格式可以根据自己更改  yyyy-MM-dd)
     * @param bz
     * @return
     */
    public static String bzToDate(String bz){
        try {
            SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
            Date date = sf.parse(bz);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            // 星期三 五月 01 22:25:49 CST 2019
            //DateFormat chinaFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.CHINA);
            return dateFormat.format(date);
        }
        catch (Exception ex) {
            return null;
        }
    }
    private static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                return HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
            } else {
                return new BigDecimal(cell.getNumericCellValue()).toString();
            }
        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            return StringUtils.trimToEmpty(cell.getStringCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            return StringUtils.trimToEmpty(cell.getCellFormula());
        } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            return "";
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
            return "ERROR";
        } else {
            return cell.toString().trim();
        }
    }
    @Resource
    private DeptService deptService;

    /**
     * 获取所属项目单号前缀
     * @param user
     * @return
     */
    public String getOperateIdOrderPrefix(User user) {
        if(user.getOperateId() != null) {
            Dept dept = new Dept();
            dept.setOperateId(user.getOperateId());
            Dept resdept = deptService.findBySpec(dept);
            if(resdept != null) {
                if(ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
                    return resdept.getOrderPrefix();
                } else {
                    return user.getAccount().substring(0, 3);
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }


    private Serializable getKey(HxStorageStockParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private HxStorageStock getOldEntity(HxStorageStockParam param) {
        return this.getById(getKey(param));
    }

    private HxStorageStock getEntity(HxStorageStockParam param) {
        HxStorageStock entity = new HxStorageStock();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<HxStorageStockResult> findsubListBySpec(HxStorageStockParam subHxStorageStockParam) {
		return this.baseMapper.findsubListBySpec(subHxStorageStockParam);
	}

	@Override
	public HxStorageStockResult sumPositionStock(HxStorageStockParam sumHxStorageStockParam) {
		return this.baseMapper.sumPositionStock(sumHxStorageStockParam);
	}

	@Override
	public HxStorageStockResult findaddOffShelf(HxStorageStockParam offShelfparam) {
		return this.baseMapper.findaddOffShelf(offShelfparam);
	}


    
    @Override
    public List<HxStorageStockResult> list(HxStorageStockParam hxStorageStockParam) {
        List<HxStorageStockResult> list2 = hxStorageStockMapper.list(hxStorageStockParam);
        return list2;
    }

    @Override
    public LayuiPageInfo findchoicePageBySpec(HxStorageStockParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findchoicePageBySpec(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findchoicePageBySpec2(HxStorageStockParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findchoicePageBySpec2(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<HxStorageStockResult> findCccapacityData(HxStorageStockResult hxStorageStockResult) {
        return this.baseMapper.findCccapacityData(hxStorageStockResult);
    }

    @Override
    public ResponseData findPageBySpec2(HxStorageStockParam hxStorageStockParam, String condition) {
        //处理生产日期的起始和终止时间
        /*if (ToolUtil.isEmpty(hxStorageStockParam.getStartTime())&&
                ToolUtil.isNotEmpty(hxStorageStockParam.getEndTime())){
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime now = LocalDateTime.now().minusYears(10);
            hxStorageStockParam.setStartTime(now.format(dtf));
        }
        if (ToolUtil.isEmpty(hxStorageStockParam.getEndTime())&&
                ToolUtil.isNotEmpty(hxStorageStockParam.getStartTime())){
            hxStorageStockParam.setEndTime(DateUtil.getDate());
        }*/
        //构建分页条件，进行分页查询，获取数据
//        Page<HxStorageStockParam> pageContext = getPageContext();
//        IPage<HxStorageStockParam> page=null;
        //按模式和单位查询
        String queryMode = hxStorageStockParam.getQueryMode();
        String stockUnit = hxStorageStockParam.getStockUnit();

        if ("kc".equals(stockUnit) && "hz".equals(queryMode)){  //库存  汇总
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);

                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getKcNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight())); //库存重量
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney())); //金额
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积
            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setKcNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }else if ("cg".equals(stockUnit) && "hz".equals(queryMode)){  //采购 汇总
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);

                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);

                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getCgNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积
            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setCgNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }else if ("cg".equals(stockUnit) && "mx".equals(queryMode)){
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);

                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getCgNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积
            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setCgNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }else if ("pf".equals(stockUnit) && "mx".equals(queryMode)){
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);

                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getPfNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积
            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setPfNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }else if ("pf".equals(stockUnit) && "hz".equals(queryMode)){
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getPfNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积

            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setPfNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);

        }else if ("ps".equals(stockUnit) && "hz".equals(queryMode)){
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getPsNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积

            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3    , BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setPsNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }else if ("ps".equals(stockUnit) && "mx".equals(queryMode)){
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);
            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getPsNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积

            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);
            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setPsNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }else {
            List<HxStorageStockParam> arrlist =hxStorageStockMapper.List2(hxStorageStockParam,condition);

            DecimalFormat df = new DecimalFormat("0.000"); // 移到循环外部
            df.setRoundingMode(RoundingMode.HALF_UP);
            HxStorageStockParam hxStorageStockParam3 = new HxStorageStockParam();
            BigDecimal kc1 = BigDecimal.ZERO; // 初始化kc1为0
            BigDecimal kc2 = BigDecimal.ZERO;
            BigDecimal kc3 = BigDecimal.ZERO;
            BigDecimal kc4 = BigDecimal.ZERO;
            BigDecimal kc5 = BigDecimal.ZERO;
            for (int i = 0; i < arrlist.size(); i++) {
                HxStorageStockParam hxStorageStockParam1 = arrlist.get(i);
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
                String baseNum = hxStorageStockParam1.getBaseNum();
                kc1 = kc1.add(new BigDecimal(baseNum));
                kc2 = kc2.add(new BigDecimal(hxStorageStockParam1.getKcNum()));
                kc3 = kc3.add(new BigDecimal(hxStorageStockParam1.getStockWeight()));
                kc4 = kc4.add(new BigDecimal(hxStorageStockParam1.getStockMoney()));
                kc5 = kc5.add(new BigDecimal(hxStorageStockParam1.getStockVolume())); //库存体积

            }
            //四舍五入保留2位
            kc1 = kc1.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc2 = kc2.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc3 = kc3.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc4 = kc4.setScale(3, BigDecimal.ROUND_HALF_UP);
            kc5 = kc5.setScale(4, BigDecimal.ROUND_HALF_UP);

            hxStorageStockParam3.setBaseNum(kc1.toString());
            hxStorageStockParam3.setKcNum(kc2.toString());
            hxStorageStockParam3.setStockWeight(kc3.toString());
            hxStorageStockParam3.setStockMoney(kc4.toString());
            hxStorageStockParam3.setStockVolume(kc5.toString());
            return ResponseData.success(hxStorageStockParam3);
        }
    }

    @Override
    public List<HxStorageStockParam> findlistAllBySpecPd(HxStorageStockParam hxStorageStockParam, String condition) {
        List<HxStorageStockParam> returnlist = new ArrayList<>();
        // 按模式和单位查询
        String queryMode = hxStorageStockParam.getQueryMode();
        String stockUnit = hxStorageStockParam.getStockUnit();
        if ("kc".equals(stockUnit) && "kw".equals(queryMode)) { // 库存，库位
            List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpecPd(hxStorageStockParam, condition);

            // 1.new一个map判断正常和待回传，将待回传的值取出来，加到正常的上面
            Map<String, HxStorageStockParam> mergedMap = new HashMap<>();

            for (HxStorageStockParam param : arrlist) {
                if ("待回传".equals(param.getStockState()) || "正常".equals(param.getStockState())) {
                    // 2.创建唯一值 (商品名+商品码 ：可乐99381)
                    String key = param.getGoodsName() + param.getGoodsNumber();
                    // 3.刚开始get返回空 都走put方法，到有待回传时，get(key) 返回的值就不为空，则加起来
                    HxStorageStockParam existingParam = mergedMap.get(key); // 获取put进去过的值
                    if (existingParam != null) {
                        // 如果已存在该商品，累加库存数量
                        BigDecimal existingQuantity = new BigDecimal(existingParam.getKcNum()); // 现有库存
                        BigDecimal currentQuantity = new BigDecimal(param.getKcNum()); // 待回传库存数量
                        BigDecimal totalQuantity = existingQuantity.add(currentQuantity);
                        existingParam.setKcNum(totalQuantity.toString());
                    } else {
                        // 如果不存在该商品，则添加新的条目
                        mergedMap.put(key, param);
                    }
                }
            }
            // map 转 集合
            List<HxStorageStockParam> mergedList = new ArrayList<>(mergedMap.values());
            // Collections.sort(要排序的列表，条件) 方法对 List 接口的实现进行排序
            // Comparator.comparing(根据什么排序) 比较器
            Collections.sort(mergedList, Comparator.comparing(HxStorageStockParam::getPositionNumber));
            // 设置返回的列表
            returnlist = mergedList;
        }
        return returnlist;
    }


    @Override
	public List<HxStorageStockParam> findlistAllBySpec(HxStorageStockParam hxStorageStockParam, String condition) {
    	List<HxStorageStockParam> returnlist = new ArrayList<HxStorageStockParam>();
        //按模式和单位查询
        String queryMode = hxStorageStockParam.getQueryMode();
        String stockUnit = hxStorageStockParam.getStockUnit();
        if ("cg".equals(stockUnit) && "mx".equals(queryMode)){//1
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec2(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);
            });
            returnlist = arrlist;
        }else if ("pf".equals(stockUnit) && "mx".equals(queryMode)){//2
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec2(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
            });
            returnlist = arrlist;
        }else if ("ps".equals(stockUnit) && "mx".equals(queryMode)){//3
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec2(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
            });
            returnlist = arrlist;
        }else if ("kc".equals(stockUnit) && "hz".equals(queryMode)){//4
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec(hxStorageStockParam,condition);
        	arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
            });
            returnlist = arrlist;
        }else if ("cg".equals(stockUnit) && "hz".equals(queryMode)){//5
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec(hxStorageStockParam,condition);
        	arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);
            });
            returnlist = arrlist;
        }else if ("pf".equals(stockUnit) && "hz".equals(queryMode)){//6
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
            });
            returnlist = arrlist;
        }else if ("ps".equals(stockUnit) && "hz".equals(queryMode)){//7
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
            });
            returnlist = arrlist;
        }else if ("kc".equals(stockUnit) && "kw".equals(queryMode)){//8 库存，库位
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec3(hxStorageStockParam,condition);
        	arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
            });
            returnlist = arrlist;
        }else if ("cg".equals(stockUnit) && "kw".equals(queryMode)){//9
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec3(hxStorageStockParam,condition);
        	arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getCgRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setCgNum(format1);
            });
            returnlist = arrlist;
        }else if ("pf".equals(stockUnit) && "kw".equals(queryMode)){//10
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec3(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPfRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPfNum(format1);
            });
            returnlist = arrlist;
        }else if ("ps".equals(stockUnit) && "kw".equals(queryMode)){//11
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec3(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal value2 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getPsRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                BigDecimal divide = multiply.divide(value2,5, BigDecimal.ROUND_HALF_DOWN);
                String format1 = df.format(divide);
                hxStorageStockParam1.setBaseNum(format);
                hxStorageStockParam1.setPsNum(format1);
            });
            returnlist = arrlist;
        }else {//0
    		List<HxStorageStockParam> arrlist = this.baseMapper.findlistAllBySpec2(hxStorageStockParam,condition);
            arrlist.forEach(hxStorageStockParam1 -> {
                BigDecimal value = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcNum()));
                BigDecimal value1 = BigDecimal.valueOf(Double.parseDouble(hxStorageStockParam1.getKcRate()));
                BigDecimal multiply = value.multiply(value1);
                DecimalFormat df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.HALF_UP);
                String format = df.format(multiply);
                hxStorageStockParam1.setBaseNum(format);
            });
            returnlist = arrlist;
        }
		return returnlist;
	}

    @Override
    public List<HxStorageStockResult> findaddOffShelflist(HxStorageStockParam offShelfparam) {
        return this.baseMapper.findaddOffShelflist(offShelfparam);
    }

    @Override
    public List<HxStorageStockResult> getPositionKcnum(HxStorageStockParam param) {
        return this.baseMapper.getPositionKcnum(param);
    }

    @Override
	public void deleteBySpec(HxStorageStockParam stockParamdelete) {
		this.baseMapper.deleteBySpec(stockParamdelete);
	}

    @Override
	public List<HxStorageStockParam> findlistAllBySpec2(HxStorageStockParam hxStorageStockParam, String condition) {
		return this.baseMapper.findlistAllBySpec2(hxStorageStockParam,condition);
	}

	@Override
	public List<HxStorageStockParam> findlistAllBySpec3(HxStorageStockParam hxStorageStockParam, String condition) {
		return this.baseMapper.findlistAllBySpec3(hxStorageStockParam,condition);
	}

    @Override
    public LayuiPageInfo dpkcQuery(DpkcQueryIby dpkcQueryIby) {

        Page pageContext = getPageContext();
        IPage page = this.baseMapper.dpkcQuery(pageContext, dpkcQueryIby);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<HxStorageStockResult> dpkcQueryAll(DpkcQueryIby dpkcQueryIby) {
        return this.baseMapper.dpkcQueryAll(dpkcQueryIby);
    }
}
